#!/usr/local/bin/node

const axios = require('axios')
const cheerio = require('cheerio')
const fs = require('fs')

const url = 'https://www.soshuw.com'
/* 
** 获取小说列表
*/
async function getNovelList() {
    const $ = cheerio.load((await axios.get(`${url}/ChuanYueZuoBaoJun/`)).data)
    const list = $('.novel_list')
    const linkArr = []

    $(list[1]).find('dd').each((index, item) => {
        const aLink = $(item).find('a')
        linkArr.push({
            name: $(aLink).text(),
            href: $(aLink).attr('href')
        })
    });
    return linkArr
}

/* 
** 获取小说内容
*/

async function getNovelContent() {
    const novelList = await getNovelList()

    for (const item of novelList) {
        const novelUrl = url + item.href

        let res = null
        try {
            res = await axios.get(novelUrl, { timeout: 20000 })
            console.log(`${item.name}下载成功`);
        } catch (error) {
            console.log(error);
            continue

        }
        const contentNode = res.data
        const $ = cheerio.load(contentNode)
        $('p').remove()
        const novelContent = $('.content').text()
        console.log(`${item.name}解析成功`);
        // console.log(novelContent);
        const result = await writeNovelData(item.name, novelContent)
        console.log(result);
    }


}

/* 
** 写入小说内容
*/
async function writeNovelData(name, data) {
    return new Promise((res, rej) => {
        const datas = name + '\n\n' + data
        fs.writeFile('./novel.txt', datas, { flag: 'a' }, (err) => {
            if (err) {
                rej(err)
            } else {
                res(`${name}写入成功`)
            }
        })
    })

}
getNovelContent()